/*=============================================================================

  MAIN FRAME MODULE

  FILE: MainFrm.h
  
  DESCRIPTION

    Main Frame module

  EXTERNALIZED FUNCTIONS


   Copyright (c) DaeSung Engineering Inc. All rights reserved.

=============================================================================*/
/*=============================================================================

  EDIT HISTORY

  ---------  -------  -------------------------------------------------------
    when       who       what, where, why
  ---------  -------  -------------------------------------------------------
   08/16/04   nso4th    Initial version

=============================================================================*/
#include "stdafx.h"
#include <Cust_AppDef.h>
#include <mainFrm.h>

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	ON_WM_CREATE()
	ON_WM_CLOSE()
	ON_MESSAGE(CUST_UI_MSG, MSG_MsgFunc)
	ON_WM_INITMENUPOPUP()
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction
CMainFrame::CMainFrame()
{
	m_mgrUI = NULL;
	m_mgrApp = NULL;
}

CMainFrame::~CMainFrame()
{
	
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

BOOL CMainFrame::WindowCreate(int x, int y, int w, int h, CWnd *pParent)
{
	DWORD		dwStyle;
	CString		szClass;
	char			szVer[128];
	char			szName[256];
	Time_s		tm;
	int64		time;

	dwStyle = WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
	szClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS, 
		::LoadCursor(NULL, IDC_ARROW), HBRUSH(COLOR_WINDOW+3), 
		::LoadIcon(NULL, IDI_APPLICATION));
	
		

	time = Time_GetTime();
	Time_GetLocalTime(time, &tm);

	UIprm_Get(UI_VERSION_SP, (dword)szVer, 128);
	sprintf(szName, "%s %s     Start Time : %00004d-%002d-%002d_%002d:%002d:%002d", CUST_APP_NAME, szVer, \
		tm.nYear, tm.nMonth, tm.nDay, tm.nHour, tm.nMin, tm.nSec);

	if(CreateEx(0, (LPCSTR)szClass, szName,
			dwStyle, CRect(x, y, x+w, y+h), pParent, 0, NULL))
		return	TRUE;

	return	FALSE;
}


/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	RECT	rc;
	HICON	hIcon;

	// Icon setup
	hIcon = AfxGetApp()->LoadIcon(ICON_MAINFRAME);
	SetIcon(hIcon, TRUE);
	
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	ObjList_Set(VIEW_MAINFRAME, this);

#ifdef TG_UI_TOOLBAR
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(ICON_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}
#endif

	// Create main view
	GetClientRect(&rc);
	if(!m_vwMain.WindowCreate(CUST_APP_NAME, rc.left, rc.right, rc.top, rc.bottom, GC_WND_CHILD, this))
	{
		return	FALSE;
	}
	appInit();
	
	return	0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	
	cs.style = WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
	cs.dwExStyle |= WS_EX_CLIENTEDGE;
	cs.lpszClass = AfxRegisterWndClass(0);

	cs.x = UIsize_TBL(UISIZE_SCRN_X);
	cs.y = UIsize_TBL(UISIZE_SCRN_Y);
	cs.cx = UIsize_TBL(UISIZE_SCRN_W);
	cs.cy = UIsize_TBL(UISIZE_SCRN_H);
	cs.hMenu = LoadMenu(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_MENU1));
	
	return TRUE;
}

BOOL CMainFrame::PreTranslateMessage(MSG* pMsg) 
{
	word	nID;

	nID = LOWORD(pMsg->wParam);
	
	switch(pMsg->message)
	{
	case WM_KEYDOWN:
	{
		SHORT		shift, ctrl;

		shift = GetKeyState(VK_SHIFT);
		ctrl = GetKeyState(VK_CONTROL);
			
		if(!((shift < 0) && (ctrl < 0)))
		{
		//	break;
		}
		switch(pMsg->wParam)
		{
		// Engineering Mode - [CTRL][SHIFT]-[F2]
		case VK_F2:
		{
			if(UIvw_IsShowEng() == FALSE)
			{
#ifndef _ODEBUG_
//				if(PWBOX_SHOW(UI_ENGPW_SP))
#endif
				{
					UIvw_Eng(TRUE);
				}
			}
			return	TRUE;
		}
		}
		break;
	}
	}

	return CFrameWnd::PreTranslateMessage(pMsg);
}

BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
{
	if(pHandlerInfo == NULL)
	{
		if(UIapp_MenuCtrl(nID, nCode, pExtra))
			return	TRUE;
	}
	
	// otherwise, do default handling
	return CFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) 
{
	return	CFrameWnd::OnCreateClient(lpcs, pContext);
}

void CMainFrame::OnClose() 
{
	SYSmgr			*sm = (SYSmgr*)ObjList_Get(SYS_MGR);
	static boolean		close_req = FALSE;
	dword			mj_sts, mn_sts;

	if(close_req == FALSE)
	{
		if(sm == NULL)
			return;
		
		mj_sts = sm->State_MajCurrent();
		mn_sts = sm->State_MinCurrent();

		if((mj_sts != IDL_S) && (mn_sts != IDL_IDL_S))
		{
			NBOX_REQ("WARNING", "SYSTEM RUNNING !!", 3);
			return;
		}
	}

	if(close_req == TRUE)
	{
		LOGCLOSE();
		appCloseDone();
		CFrameWnd::OnClose();
		return;
	}
	if(MBOX_YESNO("Are you sure to close program ?") == FALSE)
	{
		close_req = FALSE;
		return;
	}
	else
	{
		close_req = TRUE;
		appClose();
	}
}

LRESULT CMainFrame::MSG_MsgFunc(WPARAM wParam, LPARAM lParam)
{
	EHandlerEvent_s		*pEvent = (EHandlerEvent_s*)wParam;
	
	return	appMSGHandler(pEvent->dwType, pEvent->dwEvent, pEvent->wParam,
				pEvent->lParam);
}

/*--- END OF mainFrm.cpp ---*/
